package com.action;

import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

import com.bean.*;
import com.dao.*;


public class TLAddSave extends ActionSupport {

	//下面是Action内用于封装用户请求参数的属性
	private String TL_TeacherID ;
    private String TL_LabID ;
	public String getTL_TeacherID() {
		return TL_TeacherID;
	}

	public void setTL_TeacherID(String tLTeacherID) {
		TL_TeacherID = tLTeacherID;
	}

	public String getTL_LabID() {
		return TL_LabID;
	}

	public void setTL_LabID(String tLLabID) {
		TL_LabID = tLLabID;
	}

	//处理用户请求的execute方法
	public String execute() throws Exception {
		
		//解决乱码，用于页面输出
		HttpServletResponse response=null;
		response=ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		//创建session对象
		HttpSession session = ServletActionContext.getRequest().getSession();
		//验证是否正常登录
		if(session.getAttribute("id")==null){
			out.print("<script language='javascript'>alert('请重新登录！');window.location='Login.jsp';</script>");
			out.flush();out.close();return null;
		}
		
		//查询是否存在
		List<TLBean> list=new TLDao().GetList("TL_TeacherID='"+TL_TeacherID+"' and TL_LabID="+TL_LabID, "");
		if(list.size()>0)
		{
			out.print("<script language='javascript'>alert('该管理员已经在管理本实验室了，不要重复添加！');history.back(-1);</script>");
			out.flush();out.close();return null;
		}
		
		//添加
		TLBean cnbean=new TLBean();
		cnbean.setTL_TeacherID(Integer.parseInt(TL_TeacherID));
		cnbean.setTL_LabID(Integer.parseInt(TL_LabID));

		new TLDao().Add(cnbean);
		    
		//跳转
		out.print("<script language='javascript'>alert('添加成功！');window.location='TLManager.action?Lab_ID="+TL_LabID+"';</script>");
		out.flush();out.close();return null;
		
	}
	
	//判断是否空值
	private boolean isInvalid(String value) {
		return (value == null || value.length() == 0);
	}
	
	//测试
	public static void main(String[] args) {
		System.out.println();
	}
	
}
